package cn.kgc.emp.controller;

import cn.kgc.common.vo.Result;
import cn.kgc.emp.entity.Emp;
import cn.kgc.emp.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kgc
 * @since 2022-04-29
 */
@RestController
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    private IEmpService empService;

    @Value("${server.port}")
    private Integer port;

    @GetMapping("/list")
    public Result<List<Emp>> getEmpList(){
        List<Emp> list = empService.list();
        return Result.success(list);
    }

    @GetMapping("/{id}")
    public Result<Emp> getEmpById(@PathVariable("id") Integer id){
        Emp emp = empService.getById(id);
        return Result.success(emp,"服务端口：" + port);
    }

    @DeleteMapping("/{id}")
    public Result<Emp> deleteEmpById(@PathVariable("id") Integer id){
        empService.removeById(id);
        return Result.success("删除成功");
    }

    @PostMapping("")
    public Result<Emp> addEmp(@RequestBody Emp emp){
        empService.save(emp);
        return Result.success("新增成功");
    }

    @PutMapping("")
    public Result<Emp> updateEmp(@RequestBody Emp emp){
        empService.updateById(emp);
        return Result.success("修改成功");
    }
}
